import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:kunlun_gas/AppViews/EngineerOrderView/model/meter_info_model.dart';
import 'package:kunlun_gas/app_files.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(EngineerMaterAddViewState state, Dispatch dispatch,
    ViewService viewService) {
  double total = 0.0;
  if (state.selectList != null) {
    for (var item in state.selectList) {
      total += (item.price * item.number);
    }
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: '材料列表'),
    body: state.materList == null
        ? MyLoadingWidget()
        : Column(
            children: [
              Expanded(
                  child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  if (state.selectList.isEmpty) {
                    return MyEmptyItemWidget();
                  }
                  EngineerMaterList item = state.selectList[index];
                  return Container(
                    padding: EdgeInsets.only(left: 12),
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(color: ThemeConfig.lineColor))),
                    height: 55,
                    child: Row(
                      children: [
                        MyInkWellWidget(
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(12)),
                              border: Border.all(
                                  color: ThemeConfig.mainColor, width: 2),
                            ),
                            width: 24,
                            margin: EdgeInsets.only(right: 12),
                            height: 24,
                            child: Center(
                              child: Text(
                                '-',
                                style: TextStyle(
                                    color: ThemeConfig.mainColor,
                                    fontSize: 20,
                                    fontWeight: FontWeight.w800,
                                    height: 1),
                              ),
                            ),
                          ),
                          onTap: () => dispatch(
                              EngineerMaterAddViewActionCreator.onDel(item)),
                        ),
                        Expanded(
                          child: Text(
                            "${item.materName} - ${item.price}元/${item.unit}",
                            style: TextThemeConfig.Font15WithBigColor22,
                          ),
                        ),
                        GoodsNumberWidget(
                          minGoodsNumber: 1,
                          goodsNumber: item.number,

                          onTapAdd: () => dispatch(
                              EngineerMaterAddViewActionCreator.onTapNumber(
                                  item, '+')),
                          onTapMin: () => dispatch(
                              EngineerMaterAddViewActionCreator.onTapNumber(
                                  item, '-')),
                            onTapText:() async {
                              var ret = await showModalBottomSheet(
                                  context: viewService.context,
                                  builder: (BuildContext context) {
                                    return CommonUtils.routeBuildPage(context, RoutePath.editPageView,
                                        {'desc': '请输入', 'title': '','inputType':TextInputType.numberWithOptions()});
                                  },
                                  isScrollControlled: true,
                                  backgroundColor: Colors.transparent);
                              if (ret != null) {
                                if (int.tryParse(ret.toString()) != null) {
                                  item.number = int.tryParse(ret.toString());
                                }
                                dispatch(EngineerMaterAddViewActionCreator.onAction());
                              }
                            }
                        ),
                      ],
                    ),
                  );
                },
                itemCount:
                    state.selectList.isEmpty ? 1 : state.selectList.length,
              )),
              state.selectList.isEmpty
                  ? Container()
                  : Row(
                      children: [
                        Expanded(child: Container()),
                        Text(
                          '价格总计:',
                          style: TextThemeConfig.Font15WithBigColor22,
                        ),
                        Text(
                          '$total',
                          style: TextThemeConfig.Font16WithColorRed,
                        ),
                        Padding(padding: EdgeInsets.only(right: 15))
                      ],
                    ),
              Container(
                margin:
                    EdgeInsets.only(left: 15, right: 15, top: 12, bottom: 40),
                height: 50,
                decoration: BoxDecoration(
                  color: ThemeConfig.mainColor,
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: InkWell(
                  child: Align(
                    child: Text(
                      '新增材料',
                      style: TextThemeConfig.Font16WithWhite,
                    ),
                  ),
                  onTap: () =>
                      dispatch(EngineerMaterAddViewActionCreator.onAdd()),
                ),
              ),
            ],
          ),
  );
}
